package cn.itcast.filter;

import javax.servlet.*;
import java.io.IOException;

//过滤器
public class Filter01 implements Filter {
    private String encoding;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
        if(this.encoding==null||this.encoding.trim().equals("")){
            this.encoding="ios-8859-1";
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(this.encoding);
        servletResponse.setContentType("text/html;charset="+this.encoding);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
